package com.glodon.paas.document.dao.mongo;

import java.util.List;

import com.glodon.paas.document.dao.PartDao;
import com.glodon.paas.document.dao.domain.PartEntity;
import com.google.code.morphia.query.Query;

public class PartDaoMongoImpl extends MongoDBDao<PartEntity, String> implements PartDao {

    @Override
    public void ensureIndexes() {
        super.ensureIndexes();
        mongoDataStore.ensureIndex(this.entityClazz, "revisionId");
    }

    @Override
    public PartEntity getLatestPartByRevision(String revisionId) {
        Query<PartEntity> q = this.createQuery().filter("revisionId", revisionId).order("-index").limit(1);
        return q.get();
    }

    @Override
    public void delParts(String revisionId) {
        Query<PartEntity> q = this.createQuery().filter("revisionId", revisionId);
        deleteByQuery(q);
    }

    @Override
    public List<PartEntity> getPartsByRevision(String revisionId) {
        Query<PartEntity> q = this.createQuery().filter("revisionId", revisionId);
        return q.asList();
    }

    @Override
    public int getPartsCountByRevision(String revisionId) {
        Query<PartEntity> q = this.createQuery().filter("revisionId", revisionId);
        return (int) count(q);
    }
}
